﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KTXD.Entities;
using KTXD.Data;
using System.Data;

namespace UserKTXD
{
    public partial class Chi_Tiet_Sub_Chuyen_Muc : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.Params["Id"] != null)
                {
                    hienThiSubChuyenMuc(Convert.ToInt32(Request.Params["Id"]));
                    pagination.CurrentIndex = 1;
                    HienthiDSSubChuyenMuc(pagination.CurrentIndex, Convert.ToInt32(Request.Params["Id"]));
                }
            }
        }

        private void HienthiDSSubChuyenMuc(int PageNo, int idSubChuyenMuc)
        {
            //Hiển thị DS Loại Chuyên mục
            int TotalRows = 0;
            DataSet dsChuyenMuc;
            dsChuyenMuc = DataRepository.Provider.ExecuteDataSet("ChuyenMucCapHai_GetPaged", "1=1 and TrangThai=1 and IdSubChuyenMuc=" + idSubChuyenMuc, "NgayCapNhat DESC", PageNo - 1, pagination.PageSize);
            rptSubChuyenMuc.DataSource = dsChuyenMuc.Tables[0];
            rptSubChuyenMuc.DataBind();
            try
            {
                TotalRows = Convert.ToInt32(dsChuyenMuc.Tables[1].Rows[0][0]);
            }
            catch (Exception)
            {
            }
            pagination.ItemCount = TotalRows;
            if (TotalRows <= pagination.PageSize)
                pagination.Visible = false;
            else
                pagination.Visible = true;
        }

        protected void Pager1_Command(object sender, CommandEventArgs e)
        {
            try
            {
                int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
                pagination.CurrentIndex = currnetPageIndex;
                HienthiDSSubChuyenMuc(pagination.CurrentIndex, Convert.ToInt32(Request.Params["Id"]));
            }
            catch (Exception ex)
            {
                Common.WriteLog("Chi-Tiet-Sub-Chuyen-Muc.aspx.cs", "Pager1_Command()", ex.ToString());
            }
        }

        private void hienThiSubChuyenMuc(int id)
        {
            SubChuyenMuc ent_LoaiChuyenMuc = DataRepository.SubChuyenMucProvider.GetById(id);
            lblSubChuyenMuc.Text = ent_LoaiChuyenMuc.TenSubChuyenMuc;
            lblTieuDe.Text = ent_LoaiChuyenMuc.TenSubChuyenMuc;
        }

        public string GetTieuDe(object Char)
        {
            string s = Char.ToString();
            return Common.ConvertToUnSign(s);
        }

        public string GetTag(Object ob)
        {

            /*Cắt chuỗi*/
            string[] arrayStr;
                arrayStr = DataRepository.ChuyenMucCapHaiProvider.GetById(Convert.ToInt32(ob.ToString())).TagCloud.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            string chuoi = "";
            //Lấy tên đệm
            for (int i = 0; i < arrayStr.Length; i++)
            {
                if (i == arrayStr.Length - 1)
                    chuoi += "<a href='../TimKiem/" + GetTieuDe(arrayStr[i]) + ".aspx'>" + arrayStr[i] + "</a>";
                else
                    chuoi += "<a href='../TimKiem/" + GetTieuDe(arrayStr[i]) + ".aspx'>" + arrayStr[i] + "</a>,";
            }
            return chuoi;
        }

        public string GetTenTacGia(object Id)
        {
            int s = Convert.ToInt32(Id.ToString());
            ThanhVien ent_ThanhVien = DataRepository.ThanhVienProvider.GetById(s);
            return ent_ThanhVien.HoTen;
        }
    }
}